import storage from 'store'
import { register,obtainCode, resUserInfo } from '@/api/reslogin'
import { welcome } from '@/utils/util'
import { ACCESS_TOKEN, REFRESH_TOKEN } from '@/store/mutation-types'
import qs from 'qs'

const resuser = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    roleid: '',
    info: {},
    userid: '',
    departmentID: '',
    loginName: '',
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_ROLEID: (state, roleid) => {
      state.roleid = roleid
    },
    SET_USERID: (state, userid) => {
      state.userid = userid
    },
    SET_DEPARTMENTID: (state, departmentID) => {
      state.departmentID = departmentID
    },
    SET_LOGINNAME: (state, loginName) => {
      state.loginName = loginName
    }
  },

  actions: {
    // 注册
    Register ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        register(userInfo).then(response => {
          if (response.access_token) {
            const result = response
            storage.set(ACCESS_TOKEN, result.token_type + ' ' + result.access_token, 7 * 24 * 60 * 60 * 1000)
            storage.set(REFRESH_TOKEN, result.refresh_token)
            commit('SET_TOKEN', result.access_token)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    //获取验证码
    ObtainCode ({ commit }, userInfo) {
        return new Promise((resolve, reject) => {
          obtainCode(userInfo).then(response => {
            if (response.access_token) {
              const result = response
              storage.set(ACCESS_TOKEN, result.token_type + ' ' + result.access_token, 7 * 24 * 60 * 60 * 1000)
              storage.set(REFRESH_TOKEN, result.refresh_token)
              commit('SET_TOKEN', result.access_token)
            }
            resolve(response)
          }).catch(error => {
            reject(error)
          })
        })
      },

    // 获取用户信息
    resGetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        resUserInfo().then(response => {
          console.log('response',response);
          const result = response.data
          if (result.roleList && result.roleList.length > 0) {
            commit('SET_ROLES', result.roleList)
          } else {
            commit('SET_ROLES', [])
          }
          commit('SET_INFO', result)
          if (result.ZHLB == '') {
            commit('SET_ZHLB', 0)
          }else{
            console.log('result.ZHLB',result.ZHLB);
            commit('SET_ZHLB', result.ZHLB)
          }
          commit('SET_NAME', { name: result.UserName, welcome: welcome() })
          commit('SET_AVATAR', result.UploadImg)
          commit('SET_USERID', result.UserID)
          commit('SET_DEPARTMENTID', result.DeptID)
          commit('SET_LOGINNAME', result.LoginName)
            resolve(response)
          // } else {
          //   reject(new Error('getInfo: roles must be a non-null array !'))
          // }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      storage.remove(ACCESS_TOKEN)
      storage.remove(REFRESH_TOKEN)
      // return new Promise((resolve, reject) => {
      //   logout().then(() => {
      //     commit('SET_TOKEN', '')
      //     commit('SET_ROLES', [])
      //     storage.remove(ACCESS_TOKEN)
      //     storage.remove(REFRESH_TOKEN)
      //     resolve()
      //   }).catch((err) => {
      //     console.log('logout fail:', err)
      //     reject(err)
      //   })
      // })
    }

  }
}

export default resuser
